# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=collectd
pkgver=5.12.0
pkgrel=9
pkgdesc="The system statistics collection daemon"
url="https://collectd.org/"
arch="all"
license="GPL-2.0-or-later"
pkgusers="collectd"
pkggroups="collectd"
makedepends="
	autoconf
	automake
	chrpath
	curl-dev
	eudev-dev
	gpsd-dev
	hiredis-dev
	i2c-tools-dev
	iptables-dev
	jansson-dev
	libatasmart-dev
	libdbi-dev
	libgcrypt-dev
	libmemcached-dev
	libmicrohttpd-dev
	libmnl-dev
	libmodbus-dev
	libnotify-dev
	liboping-dev
	libpcap-dev
	libpq-dev
	librdkafka-dev
	libtool
	libxml2-dev
	lm-sensors-dev
	lua-dev
	mariadb-connector-c-dev
	mosquitto-dev
	net-snmp-dev
	openipmi-dev
	openldap-dev
	owfs-dev
	perl-dev
	python3-dev
	rabbitmq-c-dev
	riemann-c-client-dev
	rrdtool-dev
	varnish-dev
	yajl-dev
	zlib-dev
	"
subpackages="
	$pkgname-dev
	$pkgname-doc
	$pkgname-openrc
	$pkgname-libs
	$pkgname-perl
	$pkgname-utils
	$pkgname-plugins-all:_all:noarch
	"
install="$pkgname.pre-install $pkgname.post-upgrade"
source="https://storage.googleapis.com/collectd-tarballs/collectd-$pkgver.tar.bz2
	$pkgname-3f57541-fix-broken-t-option.patch::https://github.com/collectd/collectd/commit/3f575419e7ccb37a3b10ecc82adb2e83ff2826e1.patch
	$pkgname-dd7e4fd-nginx-add-support-for-unix-socket.patch::https://github.com/collectd/collectd/commit/dd7e4fd34042483cc90ee2762f4b507fd6637d37.patch
	$pkgname-cc565f5-nginx-add-support-for-unix-socket.patch::https://github.com/collectd/collectd/commit/cc565f5f94b117e2c64b2060627dc3fc49a8abb3.patch
	$pkgname-24bb9e2-oauth-increase-buf-size.patch::https://github.com/collectd/collectd/commit/24bb9e251969d5cf0e6eee14aad7a7e3bcc59dd8.patch
	$pkgname-91a6423-ovs_stats-fix-port-deletion.patch::https://github.com/collectd/collectd/commit/91a642385e2d068d106aa5c307de968f8b33add5.patch
	$pkgname-1483ef8-snmp-remove-des-support.patch::https://github.com/collectd/collectd/commit/1483ef8e03603bb8e1f89745325a17ba50fbbedf.patch
	gnu_source.patch
	collectd.initd
	"
options="!check"  # test_common fails

# TODO: Move /etc/collectd/collectd.conf to /etc/collectd.conf.
# TODO: Clean up defualt collectd.conf.

# secfixes:
#   5.5.2-r0:
#     - CVE-2016-6254

# <subpackage>[:<libname>...]
_plugins="
	amqp
	apache
	apcups
	ascent
	barometer
	battery
	bind
	capabilities
	ceph
	chrony
	connectivity
	curl:curl*
	dbi
	disk
	dns
	email
	exec
	gps
	hddtemp
	infiniband
	ipmi
	iptables
	log_logstash
	lua
	madwifi
	mbmon
	mcelog
	memcachec
	memcached
	modbus
	mqtt
	multimeter
	mysql
	network
	nginx
	notify_desktop
	olsrd
	onewire
	openldap
	openvpn
	ovs:ovs_*
	pcie_errors
	pinba
	ping
	postgresql
	powerdns
	python
	redis
	rrdtool:rrd*
	sensors
	serial
	smart
	snmp:snmp*
	statsd
	sysevent
	teamspeak2
	ted
	varnish
	vserver
	wireless
	write_http
	write_kafka
	write_redis
	write_riemann
	write_stackdriver
	zookeeper
	"
_flags=
case "$CARCH" in
	x86 | x86_64) _plugins="$_plugins turbostat";;
	*) _flags="--disable-turbostat";;
esac
case "$CARCH" in
	x86 | armhf | armv7 | riscv64) _flags="$_flags --disable-java";;
	*) _plugins="$_plugins java"; makedepends="$makedepends java-jdk";;
esac
case "$CARCH" in
	s390x | riscv64) _flags="$_flags --disable-virt";;
	*) _plugins="$_plugins virt"; makedepends="$makedepends libvirt-dev";;
esac
for _i in $_plugins; do
	subpackages="$subpackages $pkgname-${_i%%:*}:_plugin"
done

prepare() {
	default_prepare

	autoreconf -vif

	cat >> src/collectd.conf.in <<-EOF
	<Include "/etc/collectd.d">
	  Filter "*.conf"
	</Include>
	EOF
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc/collectd \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstate=/var \
		--with-libiptc \
		--disable-werror \
		--with-perl-bindings=INSTALLDIRS=vendor \
		--with-java=/usr/lib/jvm/default-jvm \
		\
		--enable-all-plugins \
		--disable-amqp1 \
		--disable-apple_sensors \
		--disable-aquaero \
		--disable-dcpmm \
		--disable-dpdkevents \
		--disable-dpdkstat \
		--disable-dpdk_telemetry \
		--disable-gmond \
		--disable-gpu_nvidia \
		--disable-grpc \
		--disable-intel_pmu \
		--disable-intel_rdt \
		--disable-ipstats \
		--disable-lpar \
		--disable-mic \
		--disable-netapp \
		--disable-netlink \
		--disable-netstat_udp \
		--disable-notify_email \
		--disable-nut \
		--disable-oracle \
		--disable-pf \
		--disable-redfish \
		--disable-routeros \
		--disable-sigrok \
		--disable-slurm \
		--disable-tape \
		--disable-tokyotyrant \
		--disable-write_mongodb \
		--disable-write_prometheus \
		--disable-xencpu \
		--disable-xmms \
		--disable-zone \
		$_flags
	make

	if [ -f .libs/java.so ]; then
		# Fix RPATH (there's /usr/lib/jvm/default-jvm/lib/server/jre/jre/.../jre/lib/server).
		chrpath -r "$(readlink -f /usr/lib/jvm/default-jvm/lib/server)" .libs/java.so
	fi

	# disable plugins by default since its in a subpackage
	sed -i -e 's/^LoadPlugin network/#LoadPlugin network/' \
		-e 's/^LoadPlugin rrdtool/#LoadPlugin rrdtool/' \
		src/collectd.conf
}

check() {
	make check
}

package() {
	local header path

	make DESTDIR="$pkgdir" install

	find "$pkgdir" \( -name perllocal.pod -o -name .packlist \) -delete

	chown root:collectd "$pkgdir"/etc/collectd/collectd.conf
	mkdir -p "$pkgdir"/etc/collectd.d

	install -D -m755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname


	# Install all header files to allow building out-of-tree plugins.
	# This is based on Debian.
	for path in $(find src -path src/libcollectdclient -prune \
		-o -path src/liboconfig -prune \
		-o -name '*.h' -print)
	do
		install -D -m644 "$path" "$pkgdir"/usr/include/collectd/core/${path#src/}
	done
	install -D -m644 src/liboconfig/oconfig.h -t "$pkgdir"/usr/include/collectd/liboconfig/

	cd "$pkgdir"/usr/include/collectd/

	# Update include path for collectd core header files.
	headers=$(find ./core ./liboconfig -type f -name '*.h')
	for path in $headers; do
		sed -r -i "s|(include\s+)\".*\<${path##*/}\"|\1\"collectd/${path#./}\"|" $headers
	done
}

perl() {
	pkgdesc="perl bindings to collectd"

	amove usr/lib/collectd/perl.so
	amove usr/lib/perl*
	amove usr/share/perl*
}

utils() {
	pkgdesc="$pkgname utilities"

	amove usr/bin/*
}

_all() {
	pkgdesc="Meta package for all collectd plugins"
	depends=""

	local plugin; for plugin in $_plugins; do
		depends="$depends $pkgname-${plugin%%:*}"
	done

	mkdir -p "$subpkgdir"
}

_plugin() {
	local name="${subpkgname#$pkgname-}"
	pkgdesc="$name pluin for collectd"
	depends="collectd"

	local libnames=$(printf '%s\n' $_plugins | grep "^$name:" | cut -d: -f2- | tr : ' ')
	local libname

	for libname in ${libnames:-$name}; do
		amove usr/lib/collectd/$libname.so
	done

	case "$name" in
		java) amove usr/share/collectd/java;;
	esac
}

sha512sums="
7a15e4c0ae587db4987d673a790a4f3bea1cab29ce24a506fb21fa7bd2f60bfec70867eaa1c1395cb5b0e0ae81a024fb6109b064da0ef50e7c146d2198871c70  collectd-5.12.0.tar.bz2
b8af8bb68d4da90f77cea5ed2cd751340fa2abe7e6b56e5abd52f8e79d4cd40c5a73a79cfb1e517657dd2b89169320e71f2c0061e4fbed27864fb6dc958d240e  collectd-3f57541-fix-broken-t-option.patch
1b9b2e166d3a850cb0c1f1252536783fa009a13852c8b05b93f9ec81f989978c15100d406e53f1bf61e8cc40bc87bba7e3873da624d2e62aaeec4b677d8efbe1  collectd-dd7e4fd-nginx-add-support-for-unix-socket.patch
6cac6b16ea5ed6b8297d626ba07ea0484df10d28683204ff67cf96f99aadacbee8649d9478e34283d76829613d999d4644e478e17f0d8e1724447b020eba877e  collectd-cc565f5-nginx-add-support-for-unix-socket.patch
3c3092a4f55dbefae2403d21697ff56c7f9ea79edd954e072843575a7117d07d8166ee01f34595097e3f0950cdaa150f51d97931c689dfbc98fcab55d2591793  collectd-24bb9e2-oauth-increase-buf-size.patch
aa8f9d47d4b6fcbc3e1fc9bdbedf089364bad7d6698cb76e1de2d33de7699bdd96ade6775b97ed1e98e5f3da2d7fe96ade29d480dd69568240481cd06a6b6f75  collectd-91a6423-ovs_stats-fix-port-deletion.patch
e5089707e349c10a3cfb25d6a9d52db8fdd450b404c68fe82d9a321d8f6d7bd20ba6f9ef045dd76e3251e67a2cf728cc0ac58b8c791a5fb260b74cd520608cea  collectd-1483ef8-snmp-remove-des-support.patch
3078e43fc1aa5e5d5f7042472f465cc3b077c59f43a9867ade038e59bb210f26f53bb9a1e1e8a198ca53939a4b2e03a1f05628aa54f826746f970faa36acff6a  gnu_source.patch
a286c2164af196154d017658d6e5aa3cc4f76dadf90b68c3021418979d9f0377776bb990d737ae5c916c04475725365aea8b78e3bc5e2f3679d5a99f8b9af13e  collectd.initd
"
